<?php
//Журналируемая таблица last updated 15.04.09
class CARXJournalTable  extends CARXTable
{

	function setPsevdoTbl($val)
	{
		$this->psevdoTpl=$val;
	}
	function Insert()
	{
		$regEvent=true;
		if (!trim($this->psevdoTpl))
		{
			$unPrefixedname=trim(str_replace(ARX_DB_TABLE_PREFIX,'',$this->m_tables));
		}
		else $unPrefixedname=$this->psevdoTpl;

		$table = new CARXTable('cnt_eventRegistrator',"c_action='INSERT' and c_dbtable='$unPrefixedname'");
		$eventData = $table->FetchRow();
		
		$eventFilterAll = explode(';',$eventData['c_filter']);

		if ($eventFilterAll[0])
		foreach ($eventFilterAll as $k=>$v)
		{
			$eventFilter=explode('=',$eventFilterAll[$k]);

			if ($this->m_fields[$eventFilter[0]]!=$eventFilter[1])
			{

				$regEvent = false;
			}

		}

		IF (!trim($eventData['c_filter']) || $regEvent)
		if ($eventData['id']>0)
		{

			foreach ($this->m_fields as $k=>$v)
			{
				//$this->m_fields[$k]=str_replace('"',"'",$this->m_fields[$k]);
			}
			$table = new CARXTable('cnt_registeredEvents');
			$table->SetField('c_initor',$this->m_fields[$eventData['c_initorField']]);
			$table->SetField('c_action',$eventData['id']);
			$table->SetField('c_image',str_replace('|','',$eventData['c_image']));
			$table->SetField('c_data',serialize($this->m_fields),1,1,0);
			$table->SetField('c_date',date('Y-m-d H:i'));
			$table->Insert();

		}

		parent::Insert();
	}

	function Update()
	{
		$unPrefixedname=trim(str_replace(ARX_DB_TABLE_PREFIX,'',$this->m_tables));

		$table = new CARXTable('cnt_eventRegistrator',"c_action='UPDATE' and c_dbtable='$unPrefixedname'");
		$eventData = $table->FetchRow();
		if ($eventData['id']>0)
		{
			$table = new CARXTable($this->m_tables,$this->m_filter);
			$table->SetMode(1,0);
			$oldData = $table->FetchRow();


			$table = new CARXTable('cnt_registeredEvents');
			$table->SetField('c_initor',$this->m_fields[$eventData['c_initorField']]);
			$table->SetField('c_action',$eventData['id']);
			$table->SetField('c_image',str_replace('|','',$eventData['c_image']));
			$storableArr=array('oldValues'=>$oldData,'newValues'=>$this->m_fields);
			$table->SetField('c_data',serialize($storableArr),1,1,0);
			//print_r($this->m_fields); exit;
			$table->SetField('c_date',date('Y-m-d H:i'));

			$table->Insert();

		}
		parent::Update();
	}
	function Delete()
	{
		$unPrefixedname=trim(str_replace(ARX_DB_TABLE_PREFIX,'',$this->m_tables));

		$table = new CARXTable('cnt_eventRegistrator',"c_action='DELETE' and c_dbtable='$unPrefixedname'");
		$eventData = $table->FetchRow();
		//print_r($eventData);
		if ($eventData['id']>0)
		{

			$table = new CARXTable('cnt_registeredEvents');
			$table->SetField('c_initor',$this->m_fields[$eventData['c_initorField']]);
			$table->SetField('c_action',$eventData['id']);
			$table->SetField('c_image',str_replace('|','',$eventData['c_image']));
			$table->SetField('c_data',serialize($this->m_fields),1,1,0);
			$table->SetField('c_date',date('Y-m-d H:i'));

			$table->Insert();
		}
		parent::Delete();
	}
}
?>